package velocity;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

/**
 * describe 文件工具类
 * author wencong 2022/3/3
 */
public class FileUtil {

    /**
     * 创建多级存储文件夹
     */
    public static void mkdirFile(String filePath) {
        File folder = new File(filePath);
        try {
            if (!folder.exists() && !folder.isDirectory()) {
                if (!folder.mkdirs()) {
                    throw new IOException("创建文件夹失败");
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 写入文件
     */
    public static void writer(String stringWriter, String targetFile) {
        FileWriter writer;
        try {
            // 字符串写入文件
            writer = new FileWriter(targetFile);
            writer.write(stringWriter);
            writer.flush();
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
